let commonUrl = '/api'
// if (process.env.NODE_ENV === 'development') {
//     commonUrl = '/api';
// } else if (process.env.NODE_ENV === 'debug') {
//     commonUrl = '';
// } else if (process.env.NODE_ENV === 'production') {
//     commonUrl = 'http://api.123dailu.com/';
// }

function parseJSON(response) {
    return response.json()
}

function checkStatus(response) {
    if (response.status >= 200 && response.status < 500) {
        return response
    }
    const error = new Error(response.statusText)
    error.response = response
    throw error
}

export default function request(options = {}) {
    const {data, url} = options
    options = {...options}
    options.mode = 'cors'
    delete options.url
    if (data) {
        let cloneData = JSON.stringify(data)
        delete options.data
        options.body = cloneData
    }
    options.headers = {
        // 'Authorization':Authorization,
        'Content-Type': 'application/json'
    }
    return fetch(commonUrl + url, options, {credentials: 'include'})
        .then(checkStatus)
        .then(parseJSON)
        .catch(err => ({err}))
}
